package com.pphh.demo.common;

import com.google.gson.JsonObject;
import io.jaegertracing.internal.JaegerSpan;
import io.jaegertracing.spi.Reporter;
import io.opentracing.tag.Tags;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


@Slf4j
public class TraceReporter implements Reporter {

    public void report(JaegerSpan span) {
        log.info(span.toString());

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("traceID", span.context().toTraceId());
        jsonObject.addProperty("spanID", span.context().toSpanId());
        long parentId = span.context().getParentId();
        jsonObject.addProperty("parentSpanID", Long.toHexString(parentId));
        jsonObject.addProperty("startTime", span.getStart());
        jsonObject.addProperty("duration", span.getDuration());
        jsonObject.addProperty("operationName", span.getOperationName());
        Map<String, Object> tags = span.getTags();
        for (Map.Entry<String, Object> entry : (tags.entrySet())) {
            jsonObject.addProperty("tags." + entry.getKey(), entry.getValue().toString());
        }
        log.info(jsonObject.toString());
    }

    public void close() {

    }
}
